package edu.scau.mis.web.controller;

import edu.scau.mis.pos.entity.OrderItem;
import edu.scau.mis.pos.entity.PosItem;
import edu.scau.mis.pos.service.IOrderItemService;
import edu.scau.mis.pos.vo.DashboardVo;
import edu.scau.mis.pos.vo.OrderItemVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

/**
 * 订单明细表(OrderItem)表控制层
 *
 * @author CJW
 * @since 2021-11-08 21:25:12
 */
@RestController
@RequestMapping("orderItem")
public class OrderItemController {
    /**
     * 服务对象
     */
    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 步骤1:查询读者及业务信息
     *
     * @param orderNo 读者证号
     * @return 读者、借书数量、超期图书数量信息
     */
    @GetMapping("/getOrderInfo")
    public ResponseEntity<Map<String,Object>>getOrderInfo(@RequestParam("orderNo") String orderNo) {
        return ResponseEntity.ok(this.orderItemService.getOrderAndBusinessInformationByOrderNo(orderNo));
    }

    /**
     * 步骤2:输入待借读者-图书
     *
     * @param orderItemVo 传值对象
     * @return 新增结果
     */
    @GetMapping ("/enterItem")
    public ResponseEntity<List<PosItem>> enterItem(OrderItemVo orderItemVo) {
        return ResponseEntity.ok(this.orderItemService.enterLineOrderItem(orderItemVo));
    }


    /**
     * 确认借书
     * 步骤3
     * @param orderNo 读者证号
     * @return 新增结果
     */
    @GetMapping("/confirmAdd")
    public ResponseEntity<Boolean> confirmAdd(@RequestParam("orderNo") String orderNo) {
        return ResponseEntity.ok(this.orderItemService.ConfirmAddItem(orderNo));
    }

    /**
     * 分页查询
     *
     * @param orderItem 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public ResponseEntity<List<OrderItem>> queryAll(OrderItem orderItem) {
        return ResponseEntity.ok(this.orderItemService.queryAll(orderItem));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderItem> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.orderItemService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orderItem 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<OrderItem> add(@RequestBody OrderItem orderItem) {
        return ResponseEntity.ok(this.orderItemService.insert(orderItem));
    }

    /**
     * 编辑数据
     *
     * @param orderItem 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<OrderItem> edit(@RequestBody OrderItem orderItem) {
        return ResponseEntity.ok(this.orderItemService.update(orderItem));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(this.orderItemService.deleteById(id));
    }

    @GetMapping("/page")
    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, OrderItem orderItem) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrderItem> list = this.orderItemService.queryAll(orderItem);
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * Echarts饼状图数据接口
     * @return
     */
    @GetMapping("/pie")
    public ResponseEntity<List<DashboardVo>> pie(){
        return ResponseEntity.ok(this.orderItemService.CountByCategoryId());
    }
}
